package com.leetcode.leetcode1;

/**
 * @program: 最长公共前缀
 * @author: luhd
 * @create: 2021-08-24
 */
public class Practice14 {
    public static void main(String[] args) {
        String[] strs = {"flower","flow","flight"};
        String s = longestCommonPrefix(strs);
        System.out.println(s);
    }
    public static String longestCommonPrefix(String[] strs) {
        int len = strs.length;
        for (int i = 0; i < len-1; i++) {
            strs[i+1] = compare(strs[i],strs[i+1]);
        }
        return strs[len-1];
    }
    public static String compare(String s1,String s2){
        int len = s1.length()>s2.length()?s2.length():s1.length();
        String s = "";
        for (int i = 0; i < len; i++) {
            if(s1.charAt(i)==s2.charAt(i)){
                s += s1.charAt(i);
            }else break;
        }
        return s;
    }
}
